package com.xtool.livewallpaper.transparent

import android.app.Activity
import android.app.WallpaperManager
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.hardware.Camera
import android.os.Bundle
import com.gyf.immersionbar.ImmersionBar
import com.ricky.enavigation.ENavigation
import com.ricky.epermission.runWithCameraPermission
import com.xtool.livewallpaper.R
import com.xtool.livewallpaper.WallpaperNotifyManager
import com.xtool.livewallpaper.base.BaseActivity
import com.xtool.livewallpaper.util.SystemRouterUtil
import com.xtool.livewallpaper.util.ToastUtil
import com.xtool.livewallpaper.databinding.ActivityTransparentWallpaperBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class TransparentWallpaperActivity : BaseActivity() {
    private val binding: ActivityTransparentWallpaperBinding by lazy {
        ActivityTransparentWallpaperBinding.inflate(
            layoutInflater
        )
    }


    private val hasCamera: Boolean
        get() = packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)

    private var backCameraId = -1

    override fun onStart() {
        super.onStart()
        ImmersionBar.with(this)
            .transparentStatusBar()
            .init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        runWithCameraPermission(onFailed = {
            ToastUtil.show(R.string.please_open_camera_permission)
            finish()
        }) {
            if (!hasCamera) {
                ToastUtil.show(R.string.has_not_camera)
                finish()
                return@runWithCameraPermission
            }
            initCameraId()
            if (backCameraId == -1) {
                ToastUtil.show(R.string.has_not_camera)
                finish()
                return@runWithCameraPermission
            }
            setContentView(binding.root)
            binding.btnSet.setOnClickListener {
                SystemRouterUtil.runWithAlertPermission(
                    this,
                    getString(R.string.open_alert_permission)
                ) {
                    setWallpaper()
                }
            }
            binding.btnBack.setOnClickListener { finish() }
            WallpaperNotifyManager.instance.connectWallpaperProcess()
        }
    }

    private fun initCameraId() {
        val cameraNum = Camera.getNumberOfCameras()
        val info = Camera.CameraInfo()
        for (i in 0 until cameraNum) {
            Camera.getCameraInfo(i, info)
            when (info.facing) {
                Camera.CameraInfo.CAMERA_FACING_BACK -> backCameraId = i
            }
        }
    }

    private fun setWallpaper() {
        WallpaperNotifyManager.instance.setCameraWallpaper(false)
        binding.cameraView.release()
        launch {
            delay(500)
            ENavigation.with(this@TransparentWallpaperActivity)
                .setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
                .putParcelable(
                    WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                    ComponentName(packageName, (TransparentWallpaperService::class.java.canonicalName))
                )
                .onResult { _, resultCode, _ ->
                    if (resultCode == Activity.RESULT_OK) {
                        finish()
                        ToastUtil.show(R.string.set_wallpaper_success)
                        SystemRouterUtil.goHome()
                    }
                }.navigate()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            ToastUtil.show(R.string.set_wallpaper_success)
            SystemRouterUtil.goHome()
            finish()
        }
    }

    override fun onPause() {
        binding.cameraView.release()
        super.onPause()
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0, R.anim.activity_slide_out)
    }
}